Column

Chart A

Column

Chart B

Chart C

---
title: ""
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    vertical_layout: fill
    source: embed
---

```{r setup, include=FALSE}
library(flexdashboard)
library(tidyverse)
library(httr)
library(jsonlite)

library(p8105.datasets)
library(plotly)
```


```{r}
get_all_inspections = function(url) {
  
  all_inspections = vector("list", length = 0)
  
  loop_index = 1
  chunk_size = 50000
  DO_NEXT = TRUE
  
  while (DO_NEXT) {
    message("Getting data, page ", loop_index)
    
    all_inspections[[loop_index]] = 
      GET(url,
          query = list(`$order` = "zipcode",
                       `$limit` = chunk_size,
                       `$offset` = as.integer((loop_index - 1) * chunk_size)
                       )
          ) %>%
      content("text") %>%
      fromJSON() %>%
      as_tibble()
    
    DO_NEXT = dim(all_inspections[[loop_index]])[1] == chunk_size
    loop_index = loop_index + 1
  }
  
  all_inspections
  
}

url = "https://data.cityofnewyork.us/resource/43nn-pn8j.json"

nyc_inspections = 
  get_all_inspections(url) %>%
  bind_rows() 
```

```{r}
nyc_inspections_df = 
  nyc_inspections %>% 
  select(boro, cuisine_description, inspection_date, violation_code, score, grade) %>% 
  filter(
    grade %in% c("A", "B", "C"),
    boro == "Manhattan") %>% 
  drop_na(grade)
```

Column {data-width=650}
-----------------------------------------------------------------------

### Chart A

```{r}
nyc_inspections_df %>% 
  plot_ly(
    x = ~grade, y = ~score, color = ~grade,
    type = "box", colors = "viridis")
```

Column {data-width=350}
-----------------------------------------------------------------------

### Chart B

```{r}
nyc_inspections_df %>%
  count(violation_code) %>% 
  mutate(violation_code = fct_reorder(violation_code, n)) %>% 
  plot_ly(
    x = ~n, y = ~violation_code, color = ~violation_code,
    type = "bar", colors = "viridis")
```

### Chart C

```{r}
score_distribution = 
  nyc_inspections_df %>% 
  ggplot(aes(x = score, fill = grade)) + 
  geom_density(alpha = .4, adjust = .5, color = "blue")

ggplotly(score_distribution)
```